package com.culturaltourism.admin.controller;

import com.culturaltourism.admin.pojo.SysUser;
import com.culturaltourism.admin.service.LoginService;
import com.culturaltourism.core.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LoginController
 * @Description:
 * @Author MaShuai
 * @Date 2019/5/1 20:07
 * @Version V1.0
 */
@Controller
public class LoginController implements ErrorController {

    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    /**
     * 推荐使用构造器注入，表明注入强制性，放弃原先的依赖注入。
     */
    private LoginService loginService;

    @Autowired
    public LoginController (LoginService loginService) {
        this.loginService = loginService;
    }

    /**
     * @Description 登录页面
     * @return java.lang.String
     */
    @GetMapping(value = "/login")
    public String login() {
        log.info("登录，赶紧滴登录...");
        return "login";
    }

    /**
     * @Description 主页
     * @return java.lang.String
     */
    @GetMapping(value = {"/", "/index"})
    public String index() {
        log.info("去主页喽...");
        return "index";
    }

    /**
     * @Description 实现登录
     * @Param [sysUser]
     * @return com.lingtu.core.vo.ResultVo
     */
    @PostMapping(value = "/login")
    public String login(HttpServletRequest request, SysUser sysUser) {
        log.info("登陆中...");
        return loginService.login(request, sysUser);
    }

    /**
     * @Description 退出登录
     * @return java.lang.String
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        log.info("退出登录，并消除session");
        request.getSession().invalidate();
        return "login";
    }

    /**
     * @Description 修改密码
     * @Param [request, oldPwd, newPwd]
     * @Return Result
     */
    @ResponseBody
    @RequestMapping(value = "/updatePwd")
    public Result updatePwd(HttpServletRequest request, String oldPwd, String newPwd) {
        return loginService.updatePwd(request, oldPwd, newPwd);
    }

    /**
     * 自定义错误页面
     */
    @Override
    public String getErrorPath() {
        return "/error";
    }

    /**
     * 处理错误页面
     */
    @RequestMapping("/error")
    public String handleError(Model model, HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        String errorMsg = "好像出错了呢！";
        if (statusCode == 404) {
            errorMsg = "噢，谢特！页面呢？去火星了？？？";
        }

        model.addAttribute("statusCode", statusCode);
        model.addAttribute("msg", errorMsg);
        return "/common/error";
    }
}
